package com.jukte.recipe;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ewhat.recipes.R;
import com.jukte.recipe.db.DataBaseManager;
import com.jukte.recipe.models.SubCategory;

public class SubCategoryActivity extends BaseActivity{

	private int categoryId;
	private Context con;
	private List<SubCategory> subCategories;
	private LinearLayout buttonContainer;
	private LayoutInflater inflater;
	private ImageView titleImage;
	private String catName,titleText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sub_category_layout);
		
		getIntentData();
		init();
		
		new GetSubCategoryTask(categoryId).execute();
		init();
	}
	
	@Override
	public void init() {
		super.init();
		
		ImageView adView = (ImageView) findViewById(R.id.adView);
		if(titleText.equals("Dinner Menu")){
			adView.setVisibility(View.VISIBLE);
		}else{
			adView.setVisibility(View.GONE);
		}
		
		adView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Uri uri = Uri.parse("http://e619fipmoey11w6rvap7-xfl44.hop.clickbank.net/?tid=APP");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});
		
		tv_title.setText(titleText);
		
		con = SubCategoryActivity.this;
		subCategories = new ArrayList<SubCategory>();
		buttonContainer = (LinearLayout) findViewById(R.id.button_container);
		inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		titleImage=(ImageView)findViewById(R.id.title_image);
	}
	
	private View getButton(SubCategory sub)
	{
		View v=inflater.inflate(R.layout.item_sub_category, null);
		TextView title=(TextView)v.findViewById(R.id.sub_cat_title);
		title.setText(sub.getName());
		v.setId(sub.getId());
		v.setOnClickListener(getOnClickCategoryItem(v,sub));
		return v;
		
	}
	View.OnClickListener getOnClickCategoryItem(final View button,final SubCategory sub)  {
	    return new View.OnClickListener() {
	        public void onClick(View v) {
	         Intent intent=new Intent(con, RecipeListActivity.class);
	         intent.putExtra("SUB_CAT_ID", button.getId());
	         intent.putExtra("TITLE", sub.getName()+ " Menu");
	         startActivity(intent);
	        }
	    };
	}
	private void updateView()
	{
		String resourceName="sub_cat_"+catName.toLowerCase();
		int resId = con.getResources().getIdentifier(resourceName,"drawable", getPackageName());
		titleImage.setImageResource(resId);
		buttonContainer.removeAllViews();
		for(int i=0;i<subCategories.size();i++)
		{
			buttonContainer.addView(getButton(subCategories.get(i)));
		}
	}
	
	

	private void getIntentData() {
		// TODO Auto-generated method stub
		categoryId = getIntent().getIntExtra("CATEGORY_ID", 0);
		catName=getIntent().getStringExtra("CATEGORY_NAME");
		titleText=getIntent().getStringExtra("TITLE");
	}

	

	public class GetSubCategoryTask extends AsyncTask<Void, Void, Void> {
		public ProgressDialog progDialog = null;
		private int selectedId;

		public GetSubCategoryTask(int id) {
			super();
			selectedId = id;
		}

		protected void onPreExecute() {
			progDialog = ProgressDialog.show(con, "", "Loading...", true);
		}

		@Override
		protected Void doInBackground(Void... params) {
			getSubCategories();
			return null;
		}

		private void getSubCategories() {
			subCategories = DataBaseManager.getInstance()
					.getAllSubCategoriesByCategoryId(selectedId);
			DataBaseManager.getInstance().closeDb();
		}

		protected void onPostExecute(Void result) {
			if (progDialog.isShowing()) {
				progDialog.dismiss();
			}
			updateView();
		}

	}

}
